package com.xyhuo.leetcode.december;

/**
 * 最后一个单词的长度
 * https://leetcode.cn/problems/length-of-last-word/
 *
 * @time 2023/12/25
 */
public class LengthOfLastWord {

//    public static void main(String[] args) {
//        LengthOfLastWord lolw = new LengthOfLastWord();
//        lolw.lengthOfLastWord("Today is a nice day");
//    }

    public int lengthOfLastWord(String s) {
        int ans = 0;
        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == ' ') {
                continue;
            }
            int tmp = i;
            while (true) {
                if (i + 1 == charArray.length || charArray[i + 1] == ' ') {
                    break;
                }
                i++;
            }
            ans = i - tmp + 1;
        }
        return ans;
    }
}
